| Conditions | 1 |
| Paths | 1 |
| Total Lines | 99 |
| Lines | 50 |
| Ratio | 50.51 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | const test = require('unit.js'); |
||
| 67 | process.env.JIRA_USER = 'user'; |
||
| 68 | testThat |
||
| 69 | .given() |
||
| 70 | .when(() =>jira.areJiraCredentialsMissing()) |
||
| 71 | .then((status) => expect(status).to.be.false); |
||
| 72 | }); |
||
| 73 | }); |
||
| 74 | |||
| 75 | describe('execJiraQuery', () => { |
||
| 76 | it('execJiraQuery returns parsed response on 200', () => { |
||
| 77 | sandbox.stub(requestify, 'get', () => { |
||
| 78 | return Promise.resolve({code: 200, body: '{"message": "successful!!"}'}); |
||
| 79 | }); |
||
| 80 | return expect(jira.execJiraQuery('', false)).to.eventually.have.property('message', 'successful!!'); |
||
| 81 | }); |
||
| 82 | it('execJiraQuery returns error on 400', () => { |
||
| 83 | sandbox.stub(requestify, 'get', () => { |
||
| 84 | return Promise.reject({code: 400, body: 'Failed!'}); |
||
| 85 | }); |
||
| 86 | return expect(jira.execJiraQuery('', false)).to.eventually.be.rejected; |
||
| 87 | }); |
||
| 88 | it('execJiraQuery returns error on 500', function() { |
||
| 89 | // this.timeout(8000); // need to increase Mocha timeout to give enough time to the retries |
||
| 90 | sandbox.stub(requestify, 'get', () => { |
||
| 91 | return Promise.reject({code: 500, body: 'Failed!'}); |
||
| 92 | }); |
||
| 93 | return expect(jira.execJiraQuery('', false)).to.eventually.be.rejected; |
||
| 94 | }); |
||
| 95 | it('execJiraQuery returns error on 502', function() { |
||
| 96 | // this.timeout(8000); // need to increase Mocha timeout to give enough time to the retries |
||
| 97 | sandbox.stub(requestify, 'get', () => { |
||
| 98 | return Promise.reject({code: 502, body: 'Failed!'}); |
||
| 99 | }); |
||
| 100 | return expect(jira.execJiraQuery('', false)).to.eventually.be.rejected; |
||
| 101 | }); |
||
| 102 | it('execJiraQuery fails with a not JSON response', () => { |
||
| 103 | sandbox.stub(requestify, 'get', () => { |
||
| 104 | return Promise.resolve({code: 200, body: 'Not JSON'}); |
||
| 105 | }); |
||
| 106 | return expect(jira.execJiraQuery('', false)).to.eventually.be.rejected |
||
| 107 | .then((error) => { |
||
| 108 | expect(error).to.have.property('code', -1); |
||
| 109 | expect(error).to.have.property('message', 'Unexpected token N in JSON at position 0'); |
||
| 110 | }); |
||
| 111 | }); |
||
| 112 | View Code Duplication | it('execJiraQuery aggregates results if retrieveAllPages = true and if response specifies multiple pages', () => { |
|
| 113 | let first = '{"resultset": "first"}'; |
||
| 114 | let second = '{"resultset": "second"}'; |
||
| 115 | let last = '{"resultset": "last"}'; |
||
| 116 | sandbox.stub(requestify, 'get', (url) => { |
||
| 117 | if (url === '') { |
||
| 118 | return Promise.resolve({code: 200, body: '{"startAt": 0,"total": 12,"maxResults": 5,"issues": [' + first + ']}'}); |
||
| 119 | } else if (url === '&startAt=5') { |
||
| 120 | return Promise.resolve({code: 200, body: '{"startAt": 5,"total": 12,"maxResults": 5,"issues": [' + second + ']}'}); |
||
| 121 | } else { |
||
| 122 | return Promise.resolve({code: 200, body: '{"startAt": 10,"total": 12,"maxResults": 5,"issues": [' + last + ']}'}); |
||
| 123 | } |
||
| 124 | }); |
||
| 125 | return expect(jira.execJiraQuery('', true)).to.eventually.be.fulfilled |
||
| 126 | .then((response) => { |
||
| 127 | expect(response).to.have.property('startAt', 0); |
||
| 128 | expect(response).to.have.property('total', 12); |
||
| 129 | expect(response).to.have.property('maxResults', 12); |
||
| 130 | expect(response).to.have.property('issues'); |
||
| 131 | expect(response.issues).to.be.an('array'); |
||
| 132 | expect(response.issues).to.have.length(3); |
||
| 133 | expect(response.issues[0]).to.have.property('resultset', 'first'); |
||
| 134 | expect(response.issues[1]).to.have.property('resultset', 'second'); |
||
| 135 | expect(response.issues[2]).to.have.property('resultset', 'last'); |
||
| 136 | }); |
||
| 137 | }); |
||
| 138 | View Code Duplication | it('execJiraQuery doesn\'t aggregate results if retrieveAllPages = false', () => { |
|
| 139 | let first = '{"resultset": "first"}'; |
||
| 140 | let second = '{"resultset": "second"}'; |
||
| 141 | let last = '{"resultset": "last"}'; |
||
| 142 | sandbox.stub(requestify, 'get', (url) => { |
||
| 143 | if (url === '') { |
||
| 144 | return Promise.resolve({code: 200, body: '{"startAt": 0,"total": 12,"maxResults": 5,"issues": [' + first + ']}'}); |
||
| 145 | } else if (url === '&startAt=5') { |
||
| 146 | return Promise.resolve({code: 200, body: '{"startAt": 5,"total": 12,"maxResults": 5,"issues": [' + second + ']}'}); |
||
| 147 | } else { |
||
| 148 | return Promise.resolve({code: 200, body: '{"startAt": 10,"total": 12,"maxResults": 5,"issues": [' + last + ']}'}); |
||
| 149 | } |
||
| 150 | }); |
||
| 151 | return expect(jira.execJiraQuery('', false)).to.eventually.be.fulfilled |
||
| 152 | .then((response) => { |
||
| 153 | expect(response).to.have.property('startAt', 0); |
||
| 154 | expect(response).to.have.property('total', 12); |
||
| 155 | expect(response).to.have.property('maxResults', 5); |
||
| 156 | expect(response).to.have.property('issues'); |
||
| 157 | expect(response.issues).to.be.an('array'); |
||
| 158 | expect(response.issues).to.have.length(1); |
||
| 159 | expect(response.issues[0]).to.have.property('resultset', 'first'); |
||
| 160 | }); |
||
| 161 | }); |
||
| 162 | it('execJiraQuery is rejected if failure occus during aggregation', () => { |
||
| 163 | let first = '{"resultset": "first"}'; |
||
| 164 | sandbox.stub(requestify, 'get', (url) => { |
||
| 165 | if (url === '') { |
||
| 166 | return Promise.resolve({code: 200, body: '{"startAt": 0,"total": 8,"maxResults": 5,"issues": [' + first + ']}'}); |
||
| 232 |